package com.ziilion;

import java.text.DecimalFormat;
import java.util.List;

import com.ziilion.R;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;

public class MapTabView extends MapActivity {
	 	MapView mapView;
	 	MyLocationOverlay myLocationOverlay;
	    MapController mc;
	    LocationManager lm;
	    LocationListener locationListener;
	    GeoPoint currentLoc;
	    GeoPoint homeLoc;
	    GeoPoint initLoc;
	    Bitmap bmap;
	     
	    @Override
	    public void onCreate(Bundle savedInstanceState) 
	    {
	    	//Called when the activity is first created---------
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.maptabview);
	                       
	        //Initialize Map Controls----------------
	        mapView = (MapView) findViewById(R.id.mapview);
	        mapView.setBuiltInZoomControls(true);
	        mc = mapView.getController();
	                
	        //Initialize Location on Map to CISE--------------
	        String coordinates[] = {"29.648260","-82.344182"};
	        double lat = Double.parseDouble(coordinates[0]);
	        double lng = Double.parseDouble(coordinates[1]);
	        initLoc = new GeoPoint(
	            (int) (lat * 1E6), 
	            (int) (lng * 1E6));
	        mc.animateTo(initLoc);
	        mc.setZoom(18); 
	        //mapView.setSatellite(true);
	        //mapView.setStreetView(true);
	       
	        //Activate MapOverlay to display location pins----------
	        MapOverlay mapOverlay = new MapOverlay();
	        List<Overlay> listOfOverlays = mapView.getOverlays();
	        //listOfOverlays.clear();
	        listOfOverlays.add(mapOverlay);
	        	        
	        //Add the MyLocationOverlay for current location-------------
	        MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this, mapView);
	        listOfOverlays.add(myLocationOverlay);
	        myLocationOverlay.enableMyLocation();
	        mapView.invalidate();
	        
	        //Location Manager to get and update GPS Location---------
	        lm = (LocationManager)
	        getSystemService(Context.LOCATION_SERVICE);    
	        locationListener = new MyLocationListener();
	        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
	        //lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,5000L,300F,locationListener);
	 
	    }
	    
	    public class MapOverlay extends com.google.android.maps.Overlay
	    {     
	    	GeoPoint touchLoc;
	        @Override
	        public boolean onTouchEvent(MotionEvent event, MapView mapView) 
	        {   
	            //---when user lifts his finger---
	            if (event.getAction() == 1) {
	            touchLoc = mapView.getProjection().fromPixels((int) event.getX(), (int) event.getY());
	            Toast.makeText(getBaseContext(), 
	                        touchLoc.getLatitudeE6() / 1E6 + "," + 
	                        touchLoc.getLongitudeE6() / 1E6 , 
	                        Toast.LENGTH_SHORT).show();
	            mc.animateTo(touchLoc);
	            Intent intent=new Intent();
				   
				   intent.setClass(MapTabView.this,TrafficJamInfo.class);
				   MapTabView.this.startActivity(intent);
	            //mc.setZoom(17);
	            	return true;
	            	} else return false;
	        }
	        
	        /*
	        @Override
	        public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) 
	        {
	            super.draw(canvas, mapView, shadow);                   
	 
	            //---translate the GeoPoint to screen pixels---
	            Point screenPts = new Point();
	            mapView.getProjection().toPixels(homeLoc, screenPts);
	 
	            //---add the marker---
	            Bitmap bmp = BitmapFactory.decodeResource(
	                getResources(), R.drawable.blue_dot_mylocation);            
	            canvas.drawBitmap(bmp, screenPts.x-11, screenPts.y-12, null);         
	            return true;
	        }
	        */
	    } 
	    
	    private class MyLocationListener implements LocationListener 
	    {
	        @Override
	        public void onLocationChanged(Location loc) {
	            if (loc != null) {
	            		//mapView.getProjection().fromPixels((int) loc.getLatitude(), (int) loc.getLongitude());
	            		double currentLat = loc.getLatitude();
	            		double currentLong = loc.getLongitude();
	            		DecimalFormat df = new DecimalFormat("##.######");
	            		String currentLat2 = df.format(currentLat);
	            		String currentLong2 = df.format(currentLong);
	            		
	            		//currentLoc = 
	            		//mapView.getProjection().toPixels(homeLoc, screenPts);	            		  	                      		
	                	//String currentLat1 = Double.toString(currentLat);
	                	//String currentLong1 = Double.toString(currentLong);
	                	
	                	
	                	Toast.makeText(getBaseContext(),
	                	"Current Lat: " + currentLat2 + "/n" + "Current Long: " + currentLong2, 
	                    Toast.LENGTH_SHORT).show();
	                	
	                	currentLoc = new GeoPoint(
	        	            (int) (currentLat * 1E6), 
	        	            (int) (currentLong * 1E6));
	        	        mc.animateTo(currentLoc);
	        	        mc.setZoom(17); 
	        	        //bmap = Bitmap.createBitmap(mapView.getWidth(), mapView.getHeight(), Bitmap.Config.ARGB_8888);
	        	        //Bitmap bmp = BitmapFactory.decodeResource(
	        	        //getResources(), R.drawable.blue_dot_mylocation);
	        	        //Canvas newcanvas = new Canvas(bmap);
	        	        //Canvas.drawBitmap(bmp, currentLat-11, currentLong-12, null);
	        	}
	        }

	        @Override
	        public void onProviderDisabled(String provider) {
	            // TODO Auto-generated method stub
	        }

	        @Override
	        public void onProviderEnabled(String provider) {
	            // TODO Auto-generated method stub
	        }

	        @Override
	        public void onStatusChanged(String provider, int status, 
	            Bundle extras) {
	            // TODO Auto-generated method stub
	        }
	    }       
	    
	    @Override
	    protected boolean isRouteDisplayed() {
	        // TODO Auto-generated method stub
	        return false;
	    }
}
	
	
	
